package io;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class AdHocAceDocumentParser {

    public static AceInputDocument parse(String doc) {

        String id = null;
        String sourceType = null;
        String title = ""; //not all docs have titles
        String content = null;
        String origContent = null;

        /*        Matcher headline = HEADLINE_REGEX.matcher(doc);
        if (headline.find()) {
            title = headline.group(1);
        }
        */

        Matcher docid = ID_REGEX.matcher(doc);
        if (docid.find()) {
            id = docid.group(1);
        }
        Matcher text = TEXT_REGEX.matcher(doc);
        if (text.find()) {
            content = text.group(1);
            origContent = content;
            // can I tokenize out xml?
            Matcher sgmlOut = LINGERING_SGML_REGEX.matcher(content);
            content = sgmlOut.replaceAll("");
            Matcher normalizeWhiteSpace = NORMALIZE_WHITESPACE_REGEX.matcher(content);
            content = normalizeWhiteSpace.replaceAll(" ");
        }
        Matcher source = SOURCE_REGEX.matcher(doc);
        if (source.find()) {
            sourceType = source.group(1);
        }
        return new AceInputDocument(id, sourceType, title, content, origContent);
    }
    //preserve turn taking/speaker, get speakers with regex chunker, sentence detect around.

    static final Pattern ID_REGEX = Pattern.compile("<DOCID>\\s*([^\\s<>]*)\\s*</DOCID>");
    static final Pattern HEADLINE_REGEX = Pattern.compile("<HEADLINE>(.*)</HEADLINE>",Pattern.DOTALL);
    static final Pattern TEXT_REGEX = Pattern.compile("<TEXT>(.*)</TEXT>",Pattern.DOTALL);
    static final Pattern SOURCE_REGEX = Pattern.compile("<DOCTYPE SOURCE=\"([^\"]*)\"");
    static final String SPEAKER_REGEX = "<SPEAKER>([^<>]*)</SPEAKER>";

    static final Pattern LINGERING_SGML_REGEX = Pattern.compile("<[^<>]*>",Pattern.DOTALL);
    static final Pattern NORMALIZE_WHITESPACE_REGEX = Pattern.compile("\\s+",Pattern.DOTALL);

}
